package org.apak.frontend;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apak.control.ErrorList;
import org.apak.control.ServiceDeviceForm;
import org.apak.control.ServicePartForm;
import org.apak.control.Validator;
import org.apak.db.RepairServiceDAO;
import org.apak.log.FileLogger;
import org.apak.model.Device;
import org.apak.model.LoggedSubject;
import org.apak.model.ServiceAction;
import org.apak.model.ServiceActionStatusType;
import org.apak.model.ServiceDevice;
import org.apak.model.ServiceDeviceStatusType;
import org.apak.model.ServiceType;

/**
 * Servlet implementation class ServiceDeviceServlet
 */
@WebServlet("/ServiceDeviceServlet")
public class ServiceDeviceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RepairServiceDAO dao = new RepairServiceDAO();
	private Validator validator = new Validator();
	private String serviceOrderID;
	private String id;
	private HttpSession session;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		LoggedSubject loggedSubject = (LoggedSubject) session.getAttribute("loggedSubject");
		if(loggedSubject == null || loggedSubject.isCustomer()) {
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request, response);
		} else {
			serviceOrderID = request.getParameter("serviceOrder");
			id = request.getParameter("id");
			if(id != null) {
				ServiceDevice serviceDevice = dao.getServiceDevice(id);
				request.setAttribute("serviceDevice", serviceDevice);
				serviceOrderID = Integer.toString(serviceDevice.getService_order_fk());
			}
			if(dao.getServiceOrder(serviceOrderID) == null) {
				//TODO: PRINT ERROR PAGE!!!!
			}
			if(serviceOrderID != null) {
				ServiceDeviceStatusType[] serviceDeviceStatusTypeArray = dao.getServiceDeviceStatusTypeList();
				request.setAttribute("serviceDeviceStatusTypeArray", serviceDeviceStatusTypeArray);
				request.setAttribute("serviceOrderID", serviceOrderID);
			}
			FileLogger.logMessage("Forwarded to serviceDevice.jsp by ServiceDeviceServlet.doGet().");
			RequestDispatcher dispatcher = request.getRequestDispatcher("serviceDevice.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("serviceOrderID", serviceOrderID);
		ServiceDeviceStatusType[] serviceDeviceStatusTypeArray = dao.getServiceDeviceStatusTypeList();
		request.setAttribute("serviceDeviceStatusTypeArray", serviceDeviceStatusTypeArray);
		String actionButton = request.getParameter("actionButton");
		ServiceDeviceForm serviceDeviceForm = new ServiceDeviceForm(request.getParameter("device_status"),
				request.getParameter("device_name"),
				request.getParameter("description"), serviceOrderID);
		if(actionButton != null)
			if(actionButton.equals("Save")) {
				ErrorList errors = validator.findErrors(serviceDeviceForm);
				if(errors.isErrorFound()) {
					request.setAttribute("errors", errors);
					FileLogger.logMessage("Forwarded to serviceDevice.jsp by ServiceDeviceServlet.doPost().");
					RequestDispatcher dispatcher = request.getRequestDispatcher("serviceDevice.jsp");
					dispatcher.forward(request, response);
				} else {
					if(id != null) {
						if(dao.updateServiceDevice(serviceDeviceForm, id) != 0)
							request.setAttribute("success", "Saved successfully!");
					}
					request.setAttribute("serviceDeviceForm", serviceDeviceForm);	
					FileLogger.logMessage("Forwarded to serviceDevice.jsp by ServiceDeviceServlet.doPost().");
					RequestDispatcher dispatcher = request.getRequestDispatcher("serviceDevice.jsp");
					dispatcher.forward(request, response);
				}
			}
	}

}
